var QuestionnaireManaged = function () {
	var table;
    return {
    	init:function(){
    		
    		 if (!jQuery().DataTable) {
                 return;
             }
    		 var thisObj = this;
             // begin first table
    		 table= $('#question_table').DataTable({
	             ajax: {
	             		url:"/admin/activity/vote/list",
	             		data:function(d){
	             			$.extend(d,$("#searchForm").serializeJSON());
	             	    }
	             	},
	             scrollCollapse:true,
                 columns: [
                   {data:"name"},
                   {data:"coverImg",
                	render:function(data,type,full,meta){
                		return "<img src='"+data+"' width='100px' height='80px'/>";
                	}
                   },
                   {data:"description"},
                   {data:"type",render:function(data,type,full,meta){
                	  return data=="SIGNLE"?"单选":"多选";
                    }
                   },
                   {data:null,render:function ( data, type, full, meta ) { 
	                	 var str =  "<div class='visible-md visible-lg hidden-sm hidden-xs btn-group'>"+
		                	 			"<button class='btn btn-xs btn-info' name='optionAddBtn' data-id='"+data.id+"'>"+
											"	<i class='icon-plus'>添加选项</i>" +
										 "</button>"+
										 "<button class='btn btn-xs btn-danger' name='questionRemoveBtn' data-id='"+data.id+"'>"+
											"	<i class='icon-remove'>删除</i>" +
										 "</button>"+
										"</div>"
						 return str;
                	 }
                   }
                 ]
             });
    		 $("#search").click(function(){
    			 table.draw();
    		 });
    		 $("#questionAddBtn").click(function(){
    			$("#addquestion_dialog").modal({remote:"/admin/activity/vote/addQuestionDialog"});
    		 });
    		 $("#addquestion_dialog,#maintainOption_dialog").on("hidden.bs.modal", function() {
    			    $(this).removeData("bs.modal");
    		 });
    		 $("#question_table").on("click","button[name='optionAddBtn']",function(e){
    			 var id = $(this).attr("data-id");
    			 $("#maintainOption_dialog").modal({remote:"/admin/activity/vote/maintainOptionDialog?id="+id});
    		 })
			$("#addquestion_dialog").on("click","#saveQuestionBtn",function(){
				var options ={success:function(){
					$("#addquestion_dialog").modal("hide");
					table.draw();
				}};
				$("#addQuestionForm").ajaxForm(options).submit(function(){
					return false;
				});
			})
    		 $("#question_table").on("click","[name='questionRemoveBtn']",function(){
    			 var id = $(this).attr("data-id");
    			 layer.confirm('您确定要删除该问题吗？', {
    				  btn: ['确定','取消'] //按钮
    				}, function(){
    				  $.post("/admin/activity/vote/delete",{id:id},function(data){
    					  if(data.code==200){
    						  layer.msg("删除成功")
    						  table.draw();
    					  }else{
    						  layer.msg(data.msg);
    					  }
    				  });
    				}, function(){
    					
    				});
    		 });
    		 
    		 
    		
    	}
    }
}();